package com.mlnx.push.websocket.client.utils;

import com.mlnx.common.utils.MyLog;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.nio.ByteBuffer;


public class WebSocketUtils {

    MyLog log = MyLog.getLog(getClass());

    private WebSocketListenner webSocketListenner;

    private WWebSocketClient client;
    private URI uri;

    class WWebSocketClient extends WebSocketClient {

        public WWebSocketClient(URI uri) {
            super(uri);
        }

        @Override
        public void onMessage(String message) {
            webSocketListenner.onMessage(message);
        }

        @Override
        public void onMessage(ByteBuffer blob) {

            byte[] bs = new byte[blob.remaining()];
            blob.get(bs);
            webSocketListenner.onMessage(bs);
        }

        @Override
        public void onError(Exception ex) {
            log.error(ex, "websocket 回调异常");
        }

        @Override
        public void onOpen(ServerHandshake handshake) {
            // webSocketListenner.onOpne();
        }

        @Override
        public void onClose(int code, String reason, boolean remote) {

            webSocketListenner.onClose(code, reason, remote);
        }
    }

    public WebSocketUtils(URI uri, WebSocketListenner webSocketListenner) {
        super();
        this.webSocketListenner = webSocketListenner;
        this.uri = uri;
    }

    public synchronized void sendString(String text) throws InterruptedException {

        if (client == null) {
            client = new WWebSocketClient(uri);
            boolean sucess = client.connectBlocking();
            if (!sucess){
                log.error("连接websocket:{} 失败", uri);
                client = null;
                return;
            }
        }
        try {
            client.send(text);
        } catch (Exception e) {
            log.error(e, "websocket 客户端发送数据");
            disConnect();
        }

    }

    public synchronized void sendBytes(byte[] bs) throws Exception {

        if (client == null) {
            client = new WWebSocketClient(uri);
            client.connectBlocking();
        }
        try {
            client.send(bs);
        } catch (Exception e) {
            client = new WWebSocketClient(uri);
            client.connectBlocking();
            client.send(bs);
        }

    }

    public synchronized void disConnect() {
        if (client != null) {
            client.close();
        }
        client = null;
    }

    public static void main(String[] args) throws Exception {

        URI LIFE_PORT_USR_URI = URI.create(String.format("ws://%s:%d/", "localhost", 22222));

        WebSocketUtils utils = new WebSocketUtils(LIFE_PORT_USR_URI, new WebSocketListenner() {
            @Override
            public void onMessage(String message) {
                System.out.println(message);
            }

            @Override
            public void onMessage(byte[] bs) {

            }

            @Override
            public void onClose(int code, String reason, boolean remote) {
                System.err.println(code+"   "+reason);
            }
        });

        utils.sendBytes(new byte[]{0x20, 0x02, 0x00, 0x00});

        Thread.sleep(1000000);
//        utils.client.send();

//        utils.sendString("shandong");

    }
}
